package com.taxi.driver.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.taxi.driver.R;

/**
 * 星星
 */
public class Start extends RelativeLayout {
    private LinearLayout ll;
    private int start_icon,start_empty_icon;
    private static final int MAX_LEVEL = 5;
    private LinearLayout.LayoutParams params;

    public Start(Context context) {
        this(context, null);
    }

    public Start(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Start(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Start);
        start_icon = a.getResourceId(R.styleable.Start_start_icon, R.drawable.public_star_light);
        start_empty_icon = a.getResourceId(R.styleable.Start_start_empty_icon, R.drawable.public_star_gray);
        a.recycle();
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_start_level, null);
        params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, (int) getContext().getResources().getDimension(R.dimen.padding_large), 0);
        ll = (LinearLayout) view.findViewById(R.id.ll);
        addView(view);
        setLevel(0);
    }

    /**
     * 设置等级
     * @param level
     */
    public void setLevel(double level) {
        if(level > MAX_LEVEL){
            return;
        }

        ll.removeAllViews();
        //取整数部分
        int tempZ = (int) level;

        if(level == 0){
            for(int i=0; i<MAX_LEVEL; i++){
                ImageView img = new ImageView(getContext());
                img.setLayoutParams(params);
                img.setImageResource(start_empty_icon);
                ll.addView(img);
            }
        }else{
            for(int i=0; i<tempZ; i++){
                ImageView img = new ImageView(getContext());
                img.setLayoutParams(params);
                img.setImageResource(start_icon);
                ll.addView(img);
            }
            for(int i=0; i<MAX_LEVEL-tempZ; i++){
                ImageView img = new ImageView(getContext());
                img.setLayoutParams(params);
                img.setImageResource(start_empty_icon);
                ll.addView(img);
            }
        }
    }
}
